package com.colin.base.deadlock;

import java.util.concurrent.TimeUnit;

/**
 * Created by Colin on 2020/3/22 11:00
 * email: colinzhaodong@gmail.com
 * desc: 必定发生死锁
 *
 * @author zhaod
 */
public class MustDeadLock implements Runnable{
	int flag = 1;
	static Object o1 = new Object();
	static Object o2 = new Object();


	public static void main(String[] args) {
		MustDeadLock r1 = new MustDeadLock();
		MustDeadLock r2 = new MustDeadLock();
		r1.flag = 1;
		r2.flag = 0;
		Thread t1 = new Thread(r1);
		Thread t2 = new Thread(r2);
		t1.start();
		t2.start();
	}

	@Override
	public void run() {
		System.out.println(flag);
		if (flag == 1){
			synchronized (o1){
				try {
					TimeUnit.MILLISECONDS.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (o2) {
					System.out.println("线程1成功拿到两把锁");
				}
			}
		}
		if (flag == 0){
			synchronized (o2) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (o1) {
					System.out.println("线程2成功拿到两把锁");
				}
			}
		}
	}
}
